home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 3565-4.665 / dmg-4385 / issue_10 / 15.pne < prev    next >
Text File  |  1987-04-21  |  5KB  |  128 lines

  1.                 An Article on Program optimization
  2.                  techniques by L.J.Greenhalgh
  3.  
  4.  
  5.  
  6.         Most stos games  require a fair degree of optimization if they
  7. are to  be remotely  playable.This  artice  describes a few techniques
  8. which can be used  to  wring  that  last  drop  of  speed  out of your
  9. programs.
  10.  
  11.  
  12.                 Lookup Tables
  13.  
  14.         Anyone who  has  used  the  stos  trigometric  functions  will
  15. realise how appallingly slow  they  are.One  option is to precalculate
  16. all the values you are going to need and store them in an array .It is
  17. also possible to use an integer array to store floating point values ,
  18. thus saving valuable memory. We  can  do  this  by multiplying all the
  19. floating point  values  by  a  large  integer  value  ,  32768 usually
  20. surfices.This means all are SIN values for instance can be represented
  21. in an array by the values +32768 to  -32768 as the values of SIN range
  22. from 1 to -1.Note  that  you  only  need  one  table  as  all the trig
  23. functions are related to each other by the rules
  24.  
  25.                 COS(angle)=SIN(angle+90)
  26.                 TAN(angle)=SIN(angle)/COS(angle)
  27.  
  28. The code to generate a sin table is as follows.
  29.  
  30.         10 dim sn_table(360)
  31.         20 for angle=0 to 359
  32.         30 result#=sin(rad(angle))*32768.0
  33.         40 sn_table(angle)=result#
  34.         50 next angle
  35.  
  36. to get a value from the table you use the following code
  37.  
  38.         10 rem sin (angle)
  39.         20 result#=sn_table(angle)/32768.0
  40.         30 rem cos (angle)
  41.         40 result#=sn_table((angle+90) mod 360)/32768.0
  42.  
  43.  
  44. Line 40 needs some explanation.
  45.  
  46.         We use the mod function  to  ensure  that (angle+90) is in the
  47. range 0-359.The a mod  b  function  returns  the  remainder  when a is
  48. devided by b.So 450 mod 360 is 90  ,  4  mod  2 is 0 etc.We now have a
  49. super fast way of getting at trig values.
  50.  
  51.  
  52.                 Array referencing
  53.  
  54.         Much Stos code is concerned  with manipulating the contents of
  55. arrays.Integer arrays are stored in memory as consecutive longwords (4
  56. bytes).Say we have an  array  called  missile_x(10)  which holds the x
  57. coordinates of a number of missiles in a game .When ever you reference
  58. the array Stos does the  following  calculation.You  can speed up your
  59. code by doing it directly.
  60.  
  61.         missile_x(number)=leek(varptr(missile_x(0))+number*4)
  62.  
  63.         Substituting the above code has a negligable speed increase on
  64. 1 dimensional arrays , however for  2  dimensional arrays and above it
  65. is about twice as fast! For instance :-
  66.  
  67.         dim alien_property(number_x,number_y)
  68.         v=alien_property(x,y)
  69.  
  70.         is equivalent to
  71.  
  72.         v=leek(varptr(alien_property(0,0))+x*4+(y*number_y)*4)
  73.  
  74.         When ever you start multiplying positive integers by powers of
  75. 2 , this little  trick  should  spring  to  mind.You  can  use the rol
  76. function to multiply an integer by a power of 2.Moving an integer by 1
  77. place to the left is equivalent to multiplying by 2.Moving by 2 places
  78. is equivalent to multiplying to 4  etc.Be careful not to multiply very
  79. large or negative values as you  may  start moving bits outside of the
  80. long word and you will end  up  with  garbage.You can also use the ror
  81. function to divide by powers of two.So the above example becomes.
  82.  
  83.         v=alien_property(x,y) is equivalent to
  84.  
  85.         x=rol 2,x
  86.         y=rol 2,y
  87.         v=leek(varptr(alien_property(0,0))+x+y*number_y)
  88.  
  89.         and alien_property(x,y)=v is equivalent to
  90.  
  91.         x=rol 2,x
  92.         y=rol 2,y
  93.         loke varptr(alien_property(0,0))+x+y*number_y,v
  94.  
  95.         You can speed up the above  even  more by assigning a variable
  96. to varptr(alien_property(0,0)) so it  doesn't  have  to  be worked out
  97. every time.By careful as with  the  above  code  it  is possible to go
  98. outside the bounds of the array without an error!
  99.  
  100.                 Line evaluation
  101.  
  102. Beware  of  writing  long  lines  with  many  conditions  and  complex
  103. expressions because stos will always  evaluate  the whole line.This is
  104. very inefficient if  some  of  the  conditionds  are  rarely true .For
  105. instance:-
  106.  
  107. 10 if a>b and instr(c$,"wibble",5)>0 and b>65536 then print "Oh my God
  108. this is horrible code!" else print "Is there a better way?"
  109.  
  110. If a is rarely greater than b or  b rarely greater than 65536 then the
  111. code below is much more efficient.
  112.  
  113. 10 if not(a>b and b>65536) then 30
  114. 20 if instr(c$,"wibble",5)>0 then print "Oh  my  God this code is even
  115. worse! ":goto 40
  116. 30 print "Is there a better way"
  117. 40 rest of program .....
  118.  
  119. Now    we    only     have     to    evaluate    instr$(c$,"wibble",5)
  120. occassionally.Although the above code  is  harder  to understand it is
  121. much faster and at the end of  the  day that is what is important when
  122. programming games! Check out the file  OPTIMZE.BAS for examples of the
  123. above techniques in practice.
  124.  
  125.                    Happy Stosing.
  126.  
  127.                         Les Greenhalgh
  128.